home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 May: Tool Chest / Dev.CD May 97 TC.toast / Sample Code / Snippets / Files / CustomGet unresolved alias / Source / CustomGet unresolved alias.c
Encoding:
Text File  |  1996-09-17  |  2.9 KB  |  122 lines  |  [TEXT/CWIE]

  1.     //
  2.     //    This sample demonstrates how to let the user choose an alias file
  3.     //    from an "open" dialog. The basic idea is to intercept the pseudo-
  4.     //    item 'sfHookOpenAlias' in a CustomGetFile hook function and transform
  5.     //    it into 'getOpen'. This causes the dialog to behave as if the user
  6.     //    had clicked the open button. Also, we intercept the item number of
  7.     //    a check box we have added to the dialog item template in order to
  8.     //    allow the user to choose whether to resolve aliases. Finally,
  9.     //    when CustomGetFile returns, we call Alert to let the user know
  10.     //    what happened.
  11.     //
  12.     //    Complaints and kudos to:
  13.     //
  14.     //        Pete Gontier
  15.     //        Apple Macintosh Developer Technical Support
  16.     //        <gurgle@apple.com>
  17.     //
  18.     //    Change history
  19.     //
  20.     //        01/25/95    initial version
  21.     //
  22.     //        01/30/95    use AppendDITL instead of copy of System resources
  23.     //
  24.     //        03/22/96    in DlgHookYDProc, make sure we're dealing with
  25.     //                    main dialog before doing anything interesting
  26.     //
  27.     //        03/22/96    Use an alert, not DebugStr
  28.     //
  29.  
  30. #define OLDROUTINELOCATIONS        0
  31. #define OLDROUTINENAMES            0
  32. #define SystemSevenOrLater        1
  33.  
  34. #ifndef __FONTS__
  35. #    include <Fonts.h>
  36. #endif
  37.  
  38. #ifndef __DIALOGS__
  39. #    include <Dialogs.h>
  40. #endif
  41.  
  42. #ifndef __STANDARDFILE__
  43. #    include <StandardFile.h>
  44. #endif
  45.  
  46. #ifndef __RESOURCES__
  47. #    include <Resources.h>
  48. #endif
  49.  
  50. static pascal OSErr InitMac (void)
  51. {
  52.     MaxApplZone ( );
  53.     InitGraf (&(qd.thePort));
  54.     InitFonts ( );
  55.     InitWindows ( );
  56.     InitMenus ( );
  57.     TEInit ( );
  58.     InitDialogs (nil);
  59.  
  60.     return noErr;
  61. }
  62.  
  63. static short gItemIndex_CustomGetCheckBox;
  64.  
  65. enum
  66. {
  67.     kResID_CustomGetCheckBox = 128,
  68.     kResID_ReportResultsAlert
  69. };
  70.  
  71. static pascal short DlgHookYDProc (short item, DialogRef dRef, void *)
  72. {
  73.     if (GetWRefCon (dRef) == sfMainDialogRefCon)
  74.     {
  75.         if (item == sfHookFirstCall)
  76.         {
  77.             Handle checkBoxDitlH = GetResource ('DITL',kResID_CustomGetCheckBox);
  78.             if (!ResError ( ) && checkBoxDitlH)
  79.             {
  80.                 DetachResource (checkBoxDitlH);
  81.                 if (ResError ( ))
  82.                     ReleaseResource (checkBoxDitlH);
  83.                 else
  84.                 {
  85.                     gItemIndex_CustomGetCheckBox = 1 + CountDITL (dRef);
  86.                     AppendDITL (dRef,checkBoxDitlH,appendDITLBottom);
  87.                     DisposeHandle (checkBoxDitlH);
  88.                 }
  89.             }
  90.         }
  91.         else if (item == gItemIndex_CustomGetCheckBox || item == sfHookOpenAlias)
  92.         {
  93.             short iType; Rect iRect; Handle iHandle; Boolean iValue;
  94.             GetDialogItem (dRef,gItemIndex_CustomGetCheckBox,&iType,&iHandle,&iRect);
  95.             iValue = GetControlValue ((ControlRef) iHandle);
  96.             if (item != sfHookOpenAlias)
  97.                 SetControlValue ((ControlRef) iHandle, !iValue);
  98.             else if (!iValue)
  99.                 item = getOpen;
  100.         }
  101.     }
  102.  
  103.     return item;
  104. }
  105.  
  106. void main (void)
  107. {
  108.     if (!InitMac ( ))
  109.     {
  110.         StandardFileReply sfr;
  111.         Point sfWhere = {-1,-1};
  112.         CustomGetFile (nil,-1,nil,&sfr,sfGetDialogID,sfWhere,NewDlgHookYDProc(DlgHookYDProc),nil,nil,nil,nil);
  113.  
  114.         if (sfr.sfGood)
  115.             ParamText (sfr.sfFile.name,nil,nil,nil);
  116.         else
  117.             ParamText ("\pAs the world's worst singer would say: 'No reply at all.'",nil,nil,nil);
  118.  
  119.         Alert (kResID_ReportResultsAlert,nil);
  120.     }
  121. }
  122.